
*File includes publicly available CBSA crosswalk (Feb 2013 version: https://www.census.gov/geographies/reference-files/time-series/demo/metro-micro/delineation-files.html)
*merged with ZIP code lat/long data
use "$input\Geo Crosswalk Master.dta", clear
keep cbsa state_code fips5
replace cbsa = state_code if cbsa == "00000"
ren (cbsa fips5) (hosp_cbsa hosp_fips)
contract hosp_cbsa hosp_fips
drop _f
save "$temp\hosp_cbsa.dta", replace

*Define hospital tech index var list
local techVars endoahos acuhos adtehos adtchos acardhos adulthos aclabhos iclabhos airbhos alchhos alcophos ///
alzhos ambshos arthchos asstlhos rasthos auxhos bwhthos broomhos bldohos otbonhos mammshos brnhos cichos chabhos cmngthos ///
traumhos chaphos chthhos cwellhos chihos hlthchos coutrhos comphos ctscnhos caoshos cprevhos dentshos dradfhos redshos ebcthos /// 
emdephos enbhos endorhos endouhos enrhos endoehos eswlhos frtchos fitchos opcenhos fserhos ffdmhos genhos pedhos gntchos gersvhos ///
hlthfhos hltrhos hlthshos harthos hemohos aidsshos homehhos hospchos ophoshos igrthos imprhos icarhos ipalhos imrthos icfhos imrihos ///
kdnyhos linghos livrhos lunghos mrihos mealshos msichos mohshos mscthos msctghos nichos ninthos nerohos nutrphos obhos occhshos ///
oncolhos endochos ortohos othcrhos othihos othlthos spcichos otothhos opsrghos painhos palhos pcahos patedhos patrphos pedehos ///
pedcshos pcardhos pclabhos pemerhos pedichos pelabhos rehabhos rhbophos pethos petcthos pcdephos rprshos ptonhos psyhos psycahos ///
psylshos psyedhos psyemhos psygrhos psyophos psyphhos pstrthos retirhos rbothos robohos rurlhos beamhos rsimhos specthos snhos ///
slephos socwkhos sporthos sradhos suppghos teenshos tisuhos tobhos tporthos ultsnhos urgcchos vrcshos volsvhos womhchos wmgthos

*Load AHA data characteristics from AHA data with encrypted NPIs - file available with HCCI data
foreach yr in  11 12 13 14 {

	import excel "$aha\as20`yr'fullfile_hnpi.xlsx", sheet("20`yr'") firstrow case(lower) allstring clear
	ren (hnpi mloczip mstate lat lng) (aha_hnpi hosp_zip hosp_state hosp_lat hosp_long)
	drop if aha_hnpi == ""
	drop if inlist(cntrl,"41","42","43","44","45","46","47","48")
	keep if serv == "10" | serv == "50"

	drop if (aha_hnpi == "1C0B0F79E696AEAF1394A6FF3A7AD16C" & hosp_lat == "36.258432") 
	drop if (aha_hnpi == "CD2BB574F17D71EF1A0D03189F4D55C1" & hosp_lat == "41.890432")
	drop if (aha_hnpi == "EAEBC0892C80F713176E43A8E6E14888" & hosp_lat != "35.082279")
	drop if (aha_hnpi == "F29DB5134578DE372A26C7F992BB89A7" & hosp_lat == "36.1712056")

	keep aha_hnpi hosp_zip hosp_state cntrl serv sysid mapp3 mapp5 mapp8 mapp12 mapp13 bdtot ftres admtot ipdtot mcrdc mcddc hosp_lat hosp_long fcounty hrrcode `techVars' 
	gen yr = "20`yr'"

}

order aha_hnpi hosp_zip hosp_state cntrl serv mapp3 mapp5 mapp8 mapp12 mapp13 bdtot ftres admtot ipdtot mcrdc mcddc hosp_lat hosp_long fcounty hrrcode sysid ///
mrihos cichos nichos pedichos acardhos pcardhos endoahos acuhos adtehos adtchos adulthos aclabhos iclabhos airbhos alchhos alcophos ///
alzhos ambshos arthchos asstlhos rasthos auxhos bwhthos broomhos bldohos otbonhos mammshos brnhos chabhos cmngthos ///
traumhos chaphos chthhos cwellhos chihos hlthchos coutrhos comphos ctscnhos caoshos cprevhos dentshos dradfhos redshos ebcthos /// 
emdephos enbhos endorhos endouhos enrhos endoehos eswlhos frtchos fitchos opcenhos fserhos ffdmhos genhos pedhos gntchos gersvhos ///
hlthfhos hltrhos hlthshos harthos hemohos aidsshos homehhos hospchos ophoshos igrthos imprhos icarhos ipalhos imrthos icfhos imrihos ///
kdnyhos linghos livrhos lunghos mealshos msichos mohshos mscthos msctghos ninthos nerohos nutrphos obhos occhshos ///
oncolhos endochos ortohos othcrhos othihos othlthos spcichos otothhos opsrghos painhos palhos pcahos patedhos patrphos pedehos ///
pedcshos pclabhos pemerhos pelabhos rehabhos rhbophos pethos petcthos pcdephos rprshos ptonhos psyhos psycahos ///
psylshos psyedhos psyemhos psygrhos psyophos psyphhos pstrthos retirhos rbothos robohos rurlhos beamhos rsimhos specthos snhos ///
slephos socwkhos sporthos sradhos suppghos teenshos tisuhos tobhos tporthos ultsnhos urgcchos vrcshos volsvhos womhchos wmgthos
*techVar range from endoahos-wmgthos
*keep hospital char associated with higher costs and specialized services (e.g. cardiac): mrihos cichos nichos pedichos acardhos pcardhos

*teaching = MAPP8 or more than 1 of other MAPPs
destring fcounty, replace
gen str5 hosp_fips = string(fcounty, "%05.0f")
gen teaching = mapp8 == "1"
gen mapp3_chk = mapp3 == "1"
gen mapp5_chk = mapp5 == "1"
gen mapp12_chk = mapp12 == "1"
gen mapp13_chk = mapp13 == "1"
gen mapp_check = mapp3_chk + mapp5_chk + mapp12_chk + mapp13_chk
replace teaching = 1 if mapp_check > 1
drop mapp*

foreach var in bdtot sysid cntrl admtot ipdtot mcrdc mcddc ftres hosp_lat hosp_long {
	destring `var', replace
	replace `var' = 0 if `var' == .
}

*hosp_tech variable calculation
foreach var in mrihos-wmgthos {
	destring `var', replace
}

egen hosp_tech = rowtotal(mrihos-wmgthos)
replace hosp_tech = hosp_tech/149
*replace missing with 1/149 which is equivalent to gen hosp services only
replace hosp_tech = 0.0067 if hosp_tech == 0
drop endoahos-wmgthos 

foreach var in mrihos cichos nichos pedichos acardhos pcardhos {
	replace `var' = 0 if `var' == .
}

*Add CBSA to AHA data from
drop if inlist(hosp_state, "AS", "GU", "PR", "VI")
merge m:1 hosp_fips using "$temp\hosp_cbsa.dta"
drop if _m == 2
drop _m 
replace hosp_cbsa = substr(hosp_fips, 1, 2) if hosp_cbsa == "00000"
replace hosp_cbsa = substr(hosp_fips, 1, 2) if hosp_cbsa == ""

*drop hospitals with very few beds - some are "pre-hospital" urgent care 
drop if bdtot <= 10

save "$output\aha`yr'.dta", replace

}

exit
